package org.cxn.tools.simple;

import java.io.*;

/**
 * 对象的克隆 必须实现Cloneable,Serializable
 * Created by chenxiangning on 2015/8/10.
 */
public class CloneTool<T> {
    /**
     * 串行化 汉深度复制
     *
     * @throws IOException
     */
    public Object deepClone(T source, T out) {
        try {
            //将对象写到流里
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            ObjectOutputStream oo = new ObjectOutputStream(bo);
            oo.writeObject(source);

            //从流里读出啦
            ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
            ObjectInputStream oi = new ObjectInputStream(bi);
            try {
                out = (T) oi.readObject();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return out;

    }

}
